home *** CD-ROM | disk | FTP | other *** search
- /* Conversion routines for Software Tools
- * source: convert.bds
- * version: July 28, 1981
- */
-
- #include tools.h
-
- /* ctoi - convert string at in[i] to int, increment *i.
- * increment i.
- * calling sequence: ctoi(buffer, &i);
- */
-
- ctoi(in, i)
- char *in;
- int *i;
- {
- while (in[*i] == ' ' || in[*i] == TAB) {
- (*i)++;
- }
- /* note: i still points at beginning of number */
- return(atoi(in + *i));
- }
-
- /* ctomn - translate ASCII control char to mnemonic string */
-
- /* comment out -----
- ctomn (c, rep)
- char c, rep[4]
- {
-
- int i;
- char *s;
-
- i = c & 127;
- if (0 <= i && i <= 32) {
- /* non-printing char or space */
- switch(i) {
- case 0: s = "NUL"; break;
- case 1: s = "SOH"; break;
- case 2: s = "STX"; break;
- case 3: s = "ETX"; break;
- case 4: s = "EOT"; break;
- case 5: s = "ENG"; break;
- case 6: s = "ACK"; break;
- case 7: s = "BEL"; break;
- case 8: s = "BS"; break;
- case 9: s = "HT"; break;
- case 10: s = "LF"; break;
- case 11: s = "VT"; break;
- case 12: s = "FF"; break;
- case 13: s = "CR"; break;
- case 14: s = "SO"; break;
- case 15: s = "SI"; break;
- case 16: s = "DLE"; break;
- case 17: s = "DC1"; break;
- case 18: s = "DC2"; break;
- case 19: s = "DC3"; break;
- case 20: s = "DC4"; break;
- case 21: s = "NAK"; break;
- case 22: s = "SYN"; break;
- case 23: s = "ETB"; break;
- case 24: s = "CAN"; break;
- case 25: s = "EM"; break;
- case 26: s = "SUB"; break;
- case 27: s = "ESC"; break;
- case 28: s = "FS"; break;
- case 29: s = "GS"; break;
- case 30: s = "RS"; break;
- case 31: s = "VS"; break;
- }
- scopy (s, 1, rep, 1);
- }
- else if (i == 127) {
- /* rubout (DEL) */
- scopy ("DEL", 1, rep, 1);
- }
- else {
- /* printing character */
- rep[1] = c;
- rep[2] = EOS;
- }
- return (length (rep));
- }
- ----- end comment out */
-
- /* gctoi - convert any radix string to single precision int
- * increment i.
- * calling sequence: gctoi(str, &i, radix);
- */
-
- /* comment out -----
- gctoi (str, ii, radix)
- char str[];
- int *ii, radix;
- {
-
- int base, i, v, d, j;
-
- BOOL neg
-
- i = *ii;
-
- v = 0;
- base = radix;
-
- while (str[i] == ' ' || str[i] == TAB) {
- i++;
- }
-
- neg = (str[i] == '-');
- if (str[i] == '+' || str[i] == '-') {
- i++;
- }
-
- if ( ( str[i+2] == 'r' &&
- str[i] == '1' &&
- IS_DIGIT(str[i+1])) ||
- ( str[i+1] == 'r' &&
- IS_DIGIT(str[i])
- ) {
-
- base = str[i] - '0';
- j = i;
- if (str (i + 1) != 'r') {
- j++;
- base = base * 10 + (str[j] - '0')
- }
- if (base < 2 | base > 16) {
- base = radix;
- }
- else {
- i = j + 2;
- }
- }
- for (; str[i] != EOS; i++) {
- if (IS_DIGIT(str[i])) {
- d = str[i] - '0'
- }
- else {
- d = index (digits, clower (str[i])) - 1;
- if (d < 0 || d >= base) {
- break;
- }
- v = v * base + d
- }
-
- *ii = i;
- if (neg) {
- return (-v);
- }
- else {
- return (+v);
- }
- }
- ----- end comment out */
-
- /* gitoc - convert single precision int to any radix string
- *
- */
-
- /* comment out -----
- gitoc (int, str, size, base)
- int int, size, base;
- char str [size];
- {
-
- int carry, d, i, radix, n;
- int value;
- logical unsign;
-
-
- /* digit string is generated backwards, then reversed */
- str[1] = EOS;
- if (size <= 1) {
- return[0];
- }
- radix = abs(base);
- if (radix < 2 || radix > 16) {
- radix = 10;
- }
-
- /* negative radices mean unsign conversion */
- unsign = (base < 0);
-
- if (unsign) {
- /* make positive, but keep high-order bits intact */
- n = (int / 2) & (MAX_INTERGER);
- carry = int & 1; /* get initial carry */
- }
- else {
- n = int;
- }
- i = 1;
- repeat {
- d = abs (n % radix); /* generate next digit */
- if (unsign) { /* this is only half of actual digit value */
- d = 2 * d + carry /* get actual digit value */
- if (d >= radix) { /* check for generated carry */
- d -= radix
- carry = 1
- }
- else {
- carry = 0
- }
- }
- i++;
- if (str[i] < 10) {
- str[i] += '0';
- }
- else {
- str[i] += 'A' - 10;
- }
- n /= radix
- } until (n == 0 || i >= size);
-
- if (unsign) {
- if (carry != 0 & i < size) { /* check for final carry */
- i++;
- str[i] = '1';
- }
- }
- else if (int < 0 && i < size) { /* add sign if needed */
- i++;
- str[i] = '-';
- }
-
- value = i - 1; /* will return length of string */
-
- for (d = 1; d < i; d++) { /* reverse digits */
- carry = str[d];
- str[d] = str[i];
- str[i] = carry;
- i--;
- }
- return (value);
- }
- ----- end comment out */
-
- /* mntoc - translate ASCII mnemonic into a char */
-
- /* comment out -----
- char mntoc (buf, p, default)
- char buf[], default
- int p
-
- int i, tp
- int equal
-
- char c, tmp (MAXLINE)
-
- char text (170)
- data text / _
- ACK, LETA, LETC, LETK, EOS,
- BEL, LETB, LETE, LETL, EOS,
- BS, LETB, LETS, EOS, EOS,
- CAN, LETC, LETA, LETN, EOS,
- CR, LETC, LETR, EOS, EOS,
- DC1, LETD, LETC, DIG1, EOS,
- DC2, LETD, LETC, DIG2, EOS,
- DC3, LETD, LETC, DIG3, EOS,
- DC4, LETD, LETC, DIG4, EOS,
- DEL, LETD, LETE, LETL, EOS,
- DLE, LETD, LETL, LETE, EOS,
- EM, LETE, LETM, EOS, EOS,
- ENQ, LETE, LETN, LETQ, EOS,
- EOT, LETE, LETO, LETT, EOS,
- ESC, LETE, LETS, LETC, EOS,
- ETB, LETE, LETT, LETB, EOS,
- ETX, LETE, LETT, LETX, EOS,
- FF, LETF, LETF, EOS, EOS,
- FS, LETF, LETS, EOS, EOS,
- GS, LETG, LETS, EOS, EOS,
- HT, LETH, LETT, EOS, EOS,
- LF, LETL, LETF, EOS, EOS,
- NAK, LETN, LETA, LETK, EOS,
- NUL, LETN, LETU, LETL, EOS,
- RS, LETR, LETS, EOS, EOS,
- SI, LETS, LETI, EOS, EOS,
- SO, LETS, LETO, EOS, EOS,
- SOH, LETS, LETO, LETH, EOS,
- SP, LETS, LETP, EOS, EOS,
- STX, LETS, LETT, LETX, EOS,
- SUB, LETS, LETU, LETB, EOS,
- SYN, LETS, LETY, LETN, EOS,
- US, LETU, LETS, EOS, EOS,
- VT, LETV, LETT, EOS, EOS/
-
- tp = 1
- repeat {
- tmp (tp) = buf[p]
- tp = tp + 1
- p++
- } until (! (IS_LETTER(buf[p]) | IS_DIGIT(buf[p]))
- | tp >= MAXLINE)
- tmp (tp) = EOS
-
- if (tp == 2)
- c = tmp[1]
- else {
- lower (tmp)
- for (i = 1; i < 170; i = i + 5) /* should use binary search here */
- if (equal (tmp, text (i + 1)) == YES)
- break
- if (i < 170)
- c = text[i]
- else
- c = default
- }
-
- return[c]
- }
- ----- end comment out */
-
- /* upper - fold all alphas to upper case */
-
- upper (token)
- char *token;
- {
-
- int i;
-
- for (i = 0; token[i] != EOS; i++) {
- token[i] = toupper(token[i]);
- }
- }
-
-
- /* lower - fold all letters to lower case */
-
- lower (token)
- char *token;
- {
-
- int i;
-
- for (i = 0; token [i] != EOS; i++) {
- token [i] = tolower(token [i]);
- }
- }
-
- /* ctoc - copy the from string. Truncate after len chars */
-
- ctoc (from, to, len)
- int len;
- char *from, *to;
- {
-
- int i;
-
- for (i = 0; i < len && from [i] != EOS; i++) {
- to [i] = from [i];
- }
- to [i] = EOS;
-
- return (i);
- }
-
- /* fold - fold all letters in a string to lower case */
-
- fold (token)
- char *token;
- {
- lower (token);
- }
-
- /* itoc - convert int int to char string in str */
-
- itoc (n, str, size)
- int n, size;
- char *str;
- {
-
- int l;
-
- sprintf(str, "%d", n);
- if ((l = length(str)) >= size) {
- sys_error("itoc\n");
- }
- return(l);
- }
-
- /* comment out ----------
- int d, i, intval, j, k;
-
- intval = abs (n);
- str [0] = EOS
- for (i = 1;intval == 0 || i > size - 2;i++) {
- i++;
- str [i] = '0' + (intval % 10);
- intval = intval / 10;
- }
-
- if (n < 0 && i < size) { /* then sign */
- i = i + 1
- str (i) = MINUS
- }
- itoc = i - 1
-
- for (j = 1; j < i; j = j + 1) { /* then reverse */
- k = str (i)
- str (i) = str (j)
- str (j) = k
- i = i - 1
- }
- }
- ----- end comment out */
-
- Truncate after len chars */
-
- ctoc (from, to, len)
- int len;
- char *from, *to;
- {
-
- int i;
-
- for (i = 0
- str [0] = EOS
- for (i = 1;intval == 0 || i > size - 2;i++) {
- i++;
- str [i] = '0' + (intval % 10);
- intval = intval / 10;
- }
-
- if (n < 0 && i < size) { /* then sign */
- i = i + 1
- str (i) = MINUS
- }
- itoc = i - 1
-
- for (j = 1; j < i; j = j + 1) { /* then reverse */
- k = str (i)
- str (i) = str (j)
- str (j) = k
- i = i - 1
- }
- }
- ----- end